<?php $this->beginClip('pagetitle'); ?><?php echo $merchant->name; ?><?php $this->endClip(); ?>

<div id="sub-modal" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content discount-setup">
<!--             <div id="sub-img-btn" type="button"></div> -->
            <img id="sub-img" src="" data-idmp="" />
            <div style="background-repeat: no-repeat; background-size: 100% 100%; background-image: url('/resources/frontend/images/discount-setup-alert3.png'); width: 280px; height: 402px; margin: 0 auto;"></div>
            
            <button id="closedeliver" class="close" type="button">
                <img src="/resources/frontend/images/close-btn.png" width="22" />
            </button>
        </div>
    </div>
</div>

<div id="sub-result-modal" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content discount-setup">
            <span style="font-size: 86px; color: #FFFF00; position: absolute; top: 70px; left: 50%; margin-left: -120px; width: 240px;  text-align: center; display: block;">
                <span id="discount-txt">0.00</span>
                <span style="font-size: 26px; color: #FFFF00; ">元</span>
            </span>
            
            <img src="/resources/frontend/images/discount-result-alert.png" style="width: 280px; margin-top: 20px;" />
            <button class="close" type="button" style="top: 0;">
                <img src="/resources/frontend/images/close-btn.png" width="22" />
            </button>
        </div>
    </div>
</div>

<div id="sub-empty-modal" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <h5 style="margin: 50px 0 60px 0; text-align: center;">暂无关注二维码</h5>
        </div>
    </div>
</div>

<div class="container-fluid">
	<div class="row">
		<div class="col-md-12" style="padding: 0;">
            <h5 style="text-align: center; margin-top: 25px;">
                <!-- <span class="glyphicon glyphicon-home" aria-hidden="true" style="top: 2px;"></span> -->
                <img src="/resources/frontend/images/timg.jpg" width="12" />
                <?php echo $merchant->name; ?>
            </h5>
		
            <?php $form = $this->beginWidget('CActiveForm', array(
            	'method' => 'post',
            	'action' => url('/pay/wx', array(
                    
            	)),
            )); ?>
                <?php echo CHtml::hiddenField('id_merchant', $id_merchant); ?>
                <?php echo CHtml::hiddenField('allpay_merch hx antno', $allpay_merchantno); ?>
                <?php echo CHtml::hiddenField('allpay_terminalno', $allpay_terminalno); ?>
                <div class="disp">
                    <span style="position: absolute; left: 32px; top: 37px; font-weight: bold;">订单金额(元)</span>
                
        			<input name="amount_due" id="disp-panel" class="disp-panel" type="text" readonly="readonly" value="<?php echo $amount_due; ?>" />
        			<span class="textspan" style="margin-top: 16px; font-size: 13px; color: #888; font-weight: normal;">
        			     红包：
        			     <span style="float: right">元</span>
        			     <input type="text" name="discount" style="font-size: 14px;" value="<?php echo $discount; ?>" readonly="readonly" />
        			</span>
        			
        			<span class="textspan" style="margin-top: 10px;">
        			     实际支付(元)：
        			     <input type="text" name="amount" id="disp-panel-real" value="<?php echo $amount; ?>" readonly="readonly" />
        			</span>
    			</div>
    
    			<div class="keyboard-wrap">
                    <img src="/resources/frontend/images/pvtext.png" style="margin-bottom: 12px; height: 14px;" />
    				<ul class="keyboard list-unstyled clearfix">
    					<li><a href="javascript: {}" class="key" data-key="1">1</a></li>
    					<li><a href="javascript: {}" class="key" data-key="2">2</a></li>
    					<li><a href="javascript: {}" class="key" data-key="3">3</a></li>
    					<li>
    						<a href="javascript: {}" class="key del" data-key="del"></a>
    					</li>
    					
    					<li><a href="javascript: {}" class="key" data-key="4">4</a></li>
    					<li><a href="javascript: {}" class="key" data-key="5">5</a></li>
    					<li><a href="javascript: {}" class="key" data-key="6">6</a></li>
    					<li class="paysubmit"><a href="javascript: {}" id="pay-btn" class="key">确认<br />支付</a></li>
    					
    					<li><a href="javascript: {}" class="key" data-key="7">7</a></li>
    					<li><a href="javascript: {}" class="key" data-key="8">8</a></li>
    					<li style="width: calc(25% - 1px);"><a href="javascript: {}" class="key" data-key="9">9</a></li>
    					
    					<li><a href="javascript: {}" class="key" data-key="0">0</a></li>
    					<li><a href="javascript: {}" class="key" data-key="00">00</a></li>
    					<li style="width: calc(25% - 1px);"><a href="javascript: {}" class="key" data-key="." class="dot">.</a></li>
    				</ul>
    			</div>
            <?php $this->endWidget(); ?>
		</div>
	</div>
</div>

<script>
	$(document).ready(function(){
		document.oncontextmenu=function(e){
		    //或者return false;
		    e.preventDefault();
		};
		
		$('body').css({
			 'background-color': '#F7F2F8'
		});

		$('.modal .close').on('click', function(){
			$(this).parents('.modal').modal('hide');

			if($(this).attr('id') == 'closedeliver'){
				$.ajax({
					type: 'POST', 
					url: '/pay/deliverhis', 
					data: {
						idmp: $('#sub-modal #sub-img').attr('data-idmp'), 
						action: 'close'
					}, 
					success: function(ret){
						
					},
					dataType: 'json'
				});
			}
		});

		$('#sub-img-btn').on('touchstart', function(){
			$('#sub-img').show();
		}).on('touchend', function(){
			$('#sub-img').hide();
		});
// 		$('#sub-img').on('click', function(){
// 			$(this).hide();
// 		});

		$('#sub-modal').modal({
			backdrop: 'static', 
			keyboard: false, 
			show: false
		});
		
		var timer = setInterval(function(){
			$.ajax({
				type: 'GET', 
				url: 'http://s.visastandards.com/qrcode/getIsDiscount', 
				data: {
					idWxuser: '<?php echo $this->user->id_wxuser; ?>', 
					nickname: '<?php echo $this->user->nickname_encode; ?>', 
					headImgUrl: '<?php echo $this->user->avatar_url; ?>', 
					idmp: $('#sub-modal #sub-img').attr('data-idmp'), 
					entryIdWxplatform: '<?php echo $this->user->id_wxplatform ?>'
				}, 
				jsonp: "callback",
				jsonpCallback:"isDiscountHandler",
				success: function(data){
					if(data.isdiscount){
						$('[name="discount"]').val(data.discount);
						$('#discount-txt').text(data.discount);
						$('#sub-result-modal').modal('show');

						var disppanel = $('#disp-panel').val();
						var discount = $('[name="discount"]').val();
						var realdisppanel = disppanel - discount; 
						realdisppanel = realdisppanel < 0? 0 : realdisppanel;
						$('#disp-panel-real').val(realdisppanel.toFixed(2));

						$('#sub-modal').modal('hide');
						clearInterval(timer);
					}else{
						$('[name="discount"]').val(0);
						$('#discount-txt').text(0);

						var disppanel = $('#disp-panel').val();
						var discount = $('[name="discount"]').val();
						var realdisppanel = disppanel - discount; 
						realdisppanel = realdisppanel < 0? 0 : realdisppanel;
						$('#disp-panel-real').val(realdisppanel.toFixed(2));
					}
				},
				dataType: "jsonp"
			});
		}, 2000);


		$.ajax({
			type: 'GET', 
			url: 'http://s.visastandards.com/qrcode/get', 
			data: {
				idWxuser: '<?php echo $this->user->id_wxuser; ?>', 
				nickname: '<?php echo $this->user->nickname_encode; ?>', 
				headImgUrl: '<?php echo $this->user->avatar_url; ?>'
			}, 
			jsonp: "callback",
			jsonpCallback:"getQrHandler",
			success: function(data){
				if(data.status){
					$('#sub-modal #sub-img').attr({
						src: data.qrcodeurl, 
						'data-idmp': data.idmp
					});
					$('#sub-modal').modal('show');

					$.ajax({
						type: 'POST', 
						url: '/pay/deliverhis', 
						data: {
							idmp: data.idmp, 
							action: 'open'
						}, 
						success: function(ret){
							
						},
						dataType: 'json'
					});
				}
			},
			dataType: "jsonp"
		});
		
		<?php app()->WxInterface->initWxJsConfig(array(
			'chooseWXPay', 'getBrandWCPayRequest'
		), true, false); ?>
		wx.ready(function(){
			$('#pay-btn').data('clickforbidden', false);
			setTimeout(function(){
				$('#pay-btn').on('click', function(){
					var thisObj = $(this);
					if(!$(this).data('clickforbidden')){
		    			if($('[name="amount_due"]').val() < 0.01 || isNaN($('[name="amount_due"]').val())){
		    				$('#global-modal .modal-body').text('支付金额有误');
		    				$('#global-modal').modal('show');
		    			    return false; 
		    			}else{
		    				$(this).data('clickforbidden', true);
		    				$.ajax({
								type: 'post', 
								url: '/pay/wxpayajax', 
								data: $('form').serialize(), 
								success: function(ret){
									if(ret.result){
										if(ret.pay_info == null){
											$('#global-modal .modal-body').text('支付成功');
						    				$('#global-modal').modal('show');
						    				window.location.href = '/pay/wxsuccess?amount='+$('[name="amount"]').val()+'&amount_due='+$('[name="amount_due"]').val()+'&discount='+$('[name="discount"]').val()+'&merchant_name=<?php echo $merchant->name; ?>';
										}else{
											WeixinJSBridge.invoke('getBrandWCPayRequest', ret.pay_info, function(res){
												if(res.err_msg == "get_brand_wcpay_request:ok" ){
													var discount = $('[name="discount"]').val();
													$('[name="discount"]').val(0);
													$('#discount-txt').text(0);
													window.location.href = '/pay/wxsuccess?amount='+$('[name="amount"]').val()+'&amount_due='+$('[name="amount_due"]').val()+'&discount='+discount+'&merchant_name=<?php echo $merchant->name; ?>';
												}else{
//								 					for(var i in res){
//								 		    			alert(i + ': ' + res[i]);
//								 		    		}
										    	}
											});
										}
									}else{
										$('#global-modal .modal-body').text(ret.pay_info_error);
					    				$('#global-modal').modal('show');
									}
									thisObj.data('clickforbidden', false);
								}, 
								dataType: 'json'
			    			});
		    			}
					}
				});
			}, 2000);
		});

		$('.keyboard .key[data-key="del"]').on('touchstart', function(e){  
		    e.stopPropagation();  

		    $(this).data('touchtimer', setInterval(function(){  
		    	var disppanel = $('#disp-panel').val();
		    	$('#disp-panel').val(disppanel.substr(0, disppanel.length -1));
				var discount = $('[name="discount"]').val();
				var realdisppanel = disppanel - discount; 
				realdisppanel = realdisppanel < 0? 0 : realdisppanel;
				$('#disp-panel-real').val(realdisppanel.toFixed(2));
		    }, 100));
		}).on('touchend', function(e){  
		    e.stopPropagation();  
		    clearInterval( $(this).data('touchtimer') );  
		});  
		$('.keyboard .key').on('click', function(){
			clearInterval( $('.keyboard .key[data-key="del"]').data('touchtimer') );  
			
			var keyvalue = $(this).attr('data-key');
			var disppanel = $('#disp-panel').val();
			var newvalue = disppanel;
			if(typeof(keyvalue) != 'undefined'){
				if(keyvalue == 'del'){
					newvalue = newvalue.substring(0, newvalue.length-1);
				}else if(keyvalue == '.'){
					if(newvalue.indexOf('.') === -1 && newvalue != ''){
						newvalue += keyvalue + '';
					}else if(newvalue == '0' || newvalue == ''){
						newvalue = '0.';
					}
				}else if(keyvalue == '0'){
					if(newvalue !== '0'){
						newvalue += keyvalue + '';
					}
				}else{
					if(/^\d+(\.\d{2})+$/.test(newvalue)){
						
					}else{
						if(newvalue == '0'){
							newvalue = keyvalue;
						}else{
							newvalue += keyvalue + '';
						}
					}
					
				}
				
				var discount = $('[name="discount"]').val();
				if(newvalue > 999999999){
					$('#disp-panel').val(disppanel);

					var realdisppanel = disppanel - discount; 
					realdisppanel = realdisppanel < 0? 0 : realdisppanel;
					$('#disp-panel-real').val(realdisppanel.toFixed(2));
				}else{
					$('#disp-panel').val(newvalue);
					
					var realnewvalue = newvalue - discount; 
					realnewvalue = realnewvalue < 0? 0 : realnewvalue;
					$('#disp-panel-real').val(realnewvalue.toFixed(2));
				}
			}

			return false;
		});

		<?php if(!empty($pay_info_error)): ?>
    		$('#global-modal .modal-body').text('<?php echo $pay_info_error; ?>');
    		$('#global-modal').modal('show');
		<?php endif; ?>
	});
</script>